package com.eastfair.questionnaire.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.questionnaire.dto.*;
import com.eastfair.questionnaire.entity.Answer;
import com.eastfair.questionnaire.service.AnswerService;
import com.eastfair.questionnaire.service.QuestionService;
import com.eastfair.questionnaire.vo.AnswerVO;
import com.eastfair.questionnaire.vo.QuestionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 答案
 * </p>
 *
 * @author yuwx
 * @date 2021-08-18
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/answer")
@Api(value = "Answer", tags = "答案")
public class AnswerController extends SuperController<AnswerService, Long, Answer, AnswerPageQuery, AnswerSaveDTO, AnswerUpdateDTO> {

    private AnswerService answerService;
    private QuestionService questionService;
    private EchoService echoService;
    @Autowired
    public AnswerController(AnswerService answerService, QuestionService questionService, EchoService echoService) {
        this.answerService = answerService;
        this.questionService = questionService;
        this.echoService = echoService;
    }

    @Override
    public void handlerResult(IPage<Answer> page) {
        // 想让返回值实现自动回显，请将此行代码打开
         echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<Answer> answerList = list.stream().map((map) -> {
            Answer answer = Answer.builder().build();
            //TODO 请在这里完成转换
            return answer;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(answerList));
    }
    @ApiOperation(value = "保存答案", notes = "保存答案")
    @RequestMapping(value = "/save", method = {RequestMethod.POST, RequestMethod.PUT})
    public R<Boolean> save(
            @Validated({AnswerDTO.validateBase.class, AnswerDTO.validateSave.class})
            @RequestBody AnswerDTO answer) {
        Long projectId = ContextUtil.getProjectId();
        QuestionVO questionVO = questionService.get(projectId, answer.getQuestionId());
        answerService.save(BeanUtil.toBean(questionVO, QuestionDTO.class), answer);
        return R.successDef();
    }
    @ApiOperation(value = "根据问题id查询所有答案", notes = "根据问题id查询所有答案")
    @PostMapping("/listWithLevelByQuestion")
    public R<List<AnswerVO>> listWithLevelByQuestion(
            @Validated({ AnswerDTO.validateGetByQuestion.class})
            @RequestBody AnswerDTO answerDTO) {
        Long projectId = ContextUtil.getProjectId();
        Long questionId = answerDTO.getQuestionId();
        List<AnswerVO> answerList = answerService.listByLevel(projectId, questionId);
        return R.success(answerList);
    }

}
